Ext.define('jwawa.erp.framework.MessageBar', {
    extend : "Ext.container.Container",
    alias: 'widget.messageBar',

    msgStat: 'none',  //none, info, success, error
    msgColor: 'fff',
    msgStartBgColor: 'ffff9c',
    msgErrorEndBgColor: 'ff0000',
    msgSuccessEndBgColor: '00aa00',
    msgLabel: null,
    hidden: true,
    msgEclipseLen: 200,
    layout: {
        type: 'hbox',
        align: 'stretch'
    },
    items: [
        {
            itemId: 'msg_label',
            xtype: 'label',
            text: '',
            org_text: '',
            style: "cursor: pointer",
            flex: 1
        }
    ],

    initComponent: function() {
        var me = this;
        this.callParent(arguments);

        me.msgLabel = me.down('#msg_label');
        me.on('afterrender', function(cmp, opt) {
            me.getEl().on('click', function(e, t, opt) {
                switch(me.msgStat){
                    case  'none':
                        break;
                    case 'info':
                    case 'success':
                        alertInfo(me.org_text);
                        me.hide();
                        break;
                    case 'error':
                        alertError(me.org_text);
                        me.hide();
                        break;
                }
                me.org_text = '';
            });
        });

        me.on('hide', function(obj, eOpts){
            me.org_text = '';
            me.msgLabel.setText('');
            me.msgStat = 'none';
        });
    },

    showInfoMsg: function(msg) {
        var me = this;
        me.msgStat = "info";
        me.msgLabel.setText(Ext.String.ellipsis(msg, me.msgEclipseLen, false));
        me.org_text = msg;
        me.show();
    },

    showSuccessMsg: function(msg) {
        var me = this,
            el =  me.getEl();

        el.stopAnimation();
        me.msgStat = "success";
        me.org_text = msg;
        me.msgLabel.setText(Ext.String.ellipsis(msg, me.msgEclipseLen, false));
        el.setOpacity(0);
        me.show();
        el.animate({
            duration: 500,
            from: {color: me.msgColor, backgroundColor:  me.msgStartBgColor, opacity: 0},
            to: {color: me.msgColor, backgroundColor: me.msgSuccessEndBgColor,opacity: 100}
        }).animate({
                duration: 1000
        }).animate({
            duration: 500,
            to: {opacity: 0},
            listeners:{
                afteranimate: function(obj, startTime, eOpts){
                    me.hide();
                    me.msgStat = "none";
                }
            }
        });
    },

    showErrorMsg: function(msg) {
        var me = this,
            el =  me.getEl();

        el.stopAnimation();
        me.msgStat = "error";
        me.msgLabel.setText(Ext.String.ellipsis(msg, me.msgEclipseLen, false));
        me.org_text = msg;
        el.setOpacity(0);
        me.show();
        el.animate({
            duration: 500,
            easing: 'ease-in',
            from: {color: me.msgColor, backgroundColor:  me.msgStartBgColor, opacity: 0},
            to: {color: me.msgColor, backgroundColor: me.msgErrorEndBgColor,opacity: 100}
        });

//        me.show();
//        el.animate({
//            duration: 500,
//            from: {color: me.msgColor, backgroundColor:  me.msgStartBgColor, opacity: 0},
//            to: {color: me.msgColor, backgroundColor: me.msgErrorEndBgColor,opacity: 100}
//        }).animate({
//                duration: 1000
//            }).animate({
//                duration: 500,
//                to: {opacity: 0},
//                listeners:{
//                    afteranimate: function(obj, startTime, eOpts){
//                        me.msgStat = "none";
//                    }
//                }
//            });
    }
});